package com.zto.sch.base.thread;

import org.junit.Test;

import java.util.concurrent.*;

/**
 * @program: sch-diy-parent
 * @description:
 * @author: sch
 * @create: 2021-01-09 23:26
 **/
public class CyclicBarrierTest {
    public static final CyclicBarrier cycle = new CyclicBarrier(3);

    ExecutorService executorService = Executors.newFixedThreadPool(2);

    @Test
    public void test1() throws BrokenBarrierException, InterruptedException {
        executorService.submit(() -> {
            System.out.println(Thread.currentThread().getName());
            try {
                cycle.await();
                System.out.println(Thread.currentThread().getName() + "，执行结束");
            } catch (InterruptedException | BrokenBarrierException e) {
                e.printStackTrace();
            }
        });

        executorService.submit(() -> {
            System.out.println(Thread.currentThread().getName());
            try {
                Thread.sleep(3000);
                cycle.await();
                System.out.println(Thread.currentThread().getName() + "，执行结束");
            } catch (InterruptedException | BrokenBarrierException e) {
                e.printStackTrace();
            }
        });

        new ThreadPoolExecutor(2,2,5,TimeUnit.SECONDS,null);
        cycle.await();
    }
}
